package cn.kgc.base.controller;

import cn.kgc.commons.constant.SysDictItemStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.kgc.base.entity.SysDictItem;
import cn.kgc.base.dto.SysDictItemAddDTO;
import cn.kgc.base.dto.SysDictItemUpdateDTO;
import cn.kgc.base.service.SysDictItemService;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.kgc.base.entity.SysDictItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * (SysDictItem)表控制层
 *
 * @author makejava
 * @since 2022-12-01 09:03:53
 */
@RestController
@RequestMapping("sys/Dict/Item")
@Api
@Validated
@ConditionalOnClass(ResponseBodyAdvice.class)
public class SysDictItemController {
    /**
     * 服务对象
     */
    @Resource
    private SysDictItemService sysDictItemService;

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "dictId", value = "字典的id", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "itemText", value = "名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "status", value = "状态", paramType = "query", dataType = "short")
    })

    @CrossOrigin
    public IPage<SysDictItem> selectAll(@RequestParam(defaultValue = "1") int current,
                                        @RequestParam(defaultValue = "5") int pageSize,
                                        @RequestParam Long dictId,
                                        @RequestParam(defaultValue = "") String itemText,
                                        @RequestParam(defaultValue = "1") short status) {

        LambdaQueryWrapper<SysDictItem>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictItem::getDictId,dictId);
        lambdaQueryWrapper.like(StringUtils.isNotBlank(itemText),SysDictItem::getItemText,itemText);

        lambdaQueryWrapper.eq(status!= SysDictItemStatus.NOT_CHOOSE.getStatus(),
                SysDictItem::getStatus,status);
        //按照sort排序
        lambdaQueryWrapper.orderByAsc(SysDictItem::getSortOrder);
        IPage<SysDictItem> page = new Page(current, pageSize);

        return sysDictItemService.page(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public SysDictItem selectOne(@PathVariable Serializable id) {
        return this.sysDictItemService.getById(id);
    }

    /**
     * 启用禁用某个数据字典项
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "启用禁用某个数据字典项", produces = "application/json;charset=utf-8",
            httpMethod = "PUT", response = Result.class)
    @ApiImplicitParam(name = "id", value = "数据字典项的ID", paramType = "path", dataType = "int")
    public boolean updateSysDictItemStatus(@PathVariable(name = "id") Long id){
        SysDictItem sysDictItem = sysDictItemService.getById(id);
        int status = sysDictItem.getStatus();
        //判断原来是否是启用，如果是启用，改为禁用。如果是禁用改为启用
        status=status==
                SysDictItemStatus.ENABLE.getStatus()?SysDictItemStatus.DISABLE.getStatus():SysDictItemStatus.ENABLE.getStatus();
        sysDictItem.setStatus(status);
        //状态更新
        return sysDictItemService.updateById(sysDictItem);
    }
    //使用自定义注解，翻译常量值

    /**
     * 新增数据
     *
     * @param sysDictItemDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean insert(@RequestBody SysDictItemAddDTO sysDictItemDTO) {
        SysDictItem sysDictItem = new SysDictItem();
        BeanUtils.copyProperties(sysDictItemDTO, sysDictItem);
        //新增时，默认字典项是启用状态
        sysDictItem.setStatus(SysDictItemStatus.ENABLE.getStatus());
        //todo 新增用户的 需要修改
        String createdBy="scott";
        sysDictItem.setCreateBy(createdBy);
        return this.sysDictItemService.save(sysDictItem);
    }

    /**
     * 修改数据
     *
     * @param sysDictItemDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean update(@RequestBody SysDictItemUpdateDTO sysDictItemDTO) {
        SysDictItem sysDictItem = this.sysDictItemService.getById(sysDictItemDTO.getId());
        BeanUtils.copyProperties(sysDictItemDTO, sysDictItem);

        //todo 新增用户的 需要修改
        String updatedBy="scott";
        sysDictItem.setUpdateBy(updatedBy);
        return this.sysDictItemService.updateById(sysDictItem);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.sysDictItemService.removeById(id);
    }
}
